;; space oddity rubber
.memorymap
	defaultslot 0
	slotsize $8000
	slot 0 $0000

.endme
.rombankmap
	bankstotal 1
	banksize $8000
	banks 1
.endro

.enum $c100
	frameCount db
	vBlankFlag db
	vCount db
	rubberCount db
	rotateCount db
	rotatePeriod db
	input db
	count db
.ende
.enum $c200
	paletteRAM dsb $10*$10
.ende

.bank 0 slot 0
	.org $0000
	di
	im 1
	jp Main	
BeginOrg:
.org $0008
.define VRAMTOHL $08
SendVdpCommandFromHL:
	ld a, l
	out ($bf), a
	ld a, h
	out ($bf), a
	ret
.org $0010
.define VRAMTODE $10
SendVdpCommandFromDE:
	ld a, e
	out ($bf), a
	ld a, d
	out ($bf), a
	ret
.org $0018
	ret
.org $0020
	ret
.org $0028
	ret
.org $0038
	di
	exx
	in a, ($bf); 11 read VDP status (acknowledge interrupt)
	and $80						; 7
	jp nz, VBlankInterrupt
HBlankInterrupt:
	ld de, $c000
	rst VRAMTODE
	
	ld hl, vCount
	inc (hl)

	ld a, (count)
	and $0f
	inc a
	ld h, a
	
	ld a, (vCount)
	ld l, a
	ld a, (hl)
	add a, a
	add a, a
	add a, a
	add a, a
	and $f0
	
	ld l, a
	ld h, $c2
	ld b, $10
	ld c, $be
	otir
+:	
;; 	call RotPaletteLeft
;; 	call RenderPalette
	exx
	ei
	ret
VBlankInterrupt:
	xor a
	inc a
	ld (vBlankFlag), a
	exx
	ei
	ret
;; .org $0066
;; 	ret
EndOrg:

.org $0100
.dbsin 0, 255, 1.5, 0, 0 
.org $0200
.dbsin 0, 255, 1.5, 2, 0
.org $0300
.dbsin 0, 255, 1.5, 4, 0
.org $0400
.dbsin 0, 255, 1.5, 6, 0

.org $0500
.dbsin 0, 255, 1.5, 8, 0
.org $0600
.dbsin 0, 255, 1.5, 10, 0
.org $0700
.dbsin 0, 255, 1.5, 12, 0 
.org $0800
.dbsin 0, 255, 1.5, 14, 0

.org $0900
.dbsin 0, 255, 1.5, 16, 0 
.org $0a00
.dbsin 0, 255, 1.5, 18, 0
.org $0b00
.dbsin 0, 255, 1.5, 20, 0
.org $0c00
.dbsin 0, 255, 1.5, 22, 0

.org $0d00
.dbsin 0, 255, 1.5, 24, 0
.org $0e00
.dbsin 0, 255, 1.5, 26, 0
.org $0f00
.dbsin 0, 255, 1.5, 28, 0 
.org $1000
.dbsin 0, 255, 1.5, 30, 0 

	
	
Main:
	di
	ld sp, $dff0
ZeroRAM:
	ld hl, paletteRAM
	ld (hl), $00
	ld d, h
	ld e, l
	inc de
	ld bc, $ff
	ldir
	xor a
	ld (frameCount), a
	ld (vBlankFlag), a
	ld (vCount), a
	ld (rubberCount), a
	ld (input), a
	ld (count), a
InitRam:
	ld a, $04
	ld (rotatePeriod), a
	ld (rotateCount), a
LoadVdpRegs:
	ld hl, BeginVdpRegs
	ld b, EndVdpRegs - BeginVdpRegs
	ld c, $bf
	otir
LoadPalette:
	ld hl, BeginPalette
	ld bc, EndPalette - BeginPalette
	ld de, paletteRAM
	ldir
LoadTiles:
	ld de, $4000
	rst VRAMTODE
	ld hl, BeginTiles
	ld bc, EndTiles-BeginTiles
	call Load4bbTiles
LoadMap:
	ld ix, BeginMap
	ld de, $0000
	call LoadTileMapScreen
EnableDisplay:
	ld hl, $8160
	rst VRAMTOHL
	ei
MainLoop:
BeginUpdate:	
	ld a, (frameCount)
	ld hl, input
	ld b, (hl)
	in a, ($dc)
	ld (input), a
	xor b
	ld d, a
	and $04
	jp z, +
	ld hl, count
	inc (hl)
+:	
	call RotPaletteLeftCheckPeriod
	ld a, d
	and $08
	jp z, +
	ld hl, count
	dec (hl)
+:
	;; call z, RotPaletteRightCheckPeriod
BeginUpdatePalette:	
	ld hl, paletteRAM+$02
	ld de, paletteRAM+$11
	ld bc, $0b
	ldir
	ld a, l
	and $f0
	inc a
	ld l, a
	ldi

	.repeat $0e
	ld a, l
	and $f0
	add a, $12
	ld l, a

	ld a, e
	and $f0
	add a, $11
	ld e, a
	ld bc, $0b
	ldir

	ld a, l
	and $f0
	inc a
	ld l, a
	ldi
	.endr
	
	
EndUpdatePalette:
UpdateFrameCount:
	ld hl, frameCount
	inc (hl)
EndUpdate:
BeginRender:
WaitVBlank:
	ld a, (vBlankFlag)
	and a
	jr nz, WaitVBlankEnd
	halt
	jr z, WaitVBlank
WaitVBlankEnd:
	di	
	xor a
	ld (vBlankFlag), a
	ld (vCount), a
	;; call RenderPalette
	ei
EndRender:
	jp MainLoop
EndMainLoop:
	
RotPaletteLeftCheckPeriod:
	ld hl, rotateCount
	dec (hl)
	ret nz
	ld a, (rotatePeriod)
	ld (hl), a
RotPaletteLeft:
	ld hl, paletteRAM+1
	ld de, paletteRAM+0
	ld bc, $0c
	ldir

	ld hl, paletteRAM
	ldi
	dec hl
	ld (hl), $00
	ret
RotPaletteRightCheckPeriod:
	ld hl, rotateCount
	dec (hl)
	ret nz
	ld a, (rotatePeriod)
	ld (hl), a
	ret
RotPaletteRight:
	ld hl, paletteRAM+12
	ld de, paletteRAM+13
	ld bc, $0c
	lddr

	ld hl, paletteRAM+13
	ldd
	inc hl
	ld (hl), $00
	ret
RenderPalette:
BeginRenderPalette:
	ld de, $c000
	rst VRAMTODE
	ld hl, paletteRAM
	ld b, $10
	ld c, $be
	otir
EndRenderPalette:
	ret
Load4bbTiles:
	-:
	ld a, (hl)
	out ($be), a
	inc hl
	
	dec bc
	ld a, b
	or c
	jp nz, -
	ret
LoadTileMapScreen:
	ld a, $00
	out ($bf), a
	ld a, $78
	out ($bf), a
	
	ld bc,32*24
-:
	ld l, (ix+0)
	ld h, (ix+1)
	add hl, de
	inc ix
	inc ix
	
	ld a, l
	out ($be), a
	ld a, h
	out ($be), a
	
	dec bc
	ld a, b
	or c
	jr nz, -
	
	ret	
BeginVdpRegs:
.db $34,$A0,$20,$81,$0E,$82,$ff,$83,$07,$84,$ff,$85,$03,$86,$00,$87,$00,$88,$00,$89,$00,$8a
EndVdpRegs:
BeginPalette:
.include "gfx/ruber2 (palette).inc"
EndPalette:
BeginTiles:
.include "gfx/ruber2 (tiles).inc"
EndTiles:
BeginMap:
.include "gfx/ruber2 (tile numbers).inc"
EndMap: